{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "import struct\n",
      "import pyaudio\n",
      "import wave\n",
      "\n",
      "CHUNK = 1024\n",
      "CHANNELS = 1\n",
      "RATE = 44100\n",
      "FORMAT = pyaudio.paInt16\n",
      "WIDTH = 2\n",
      "\n",
      "FILENAME = \"tone.wav\"\n",
      "\n",
      "def play_frames(frames, chunk, channels, framerate, sample_width):\n",
      "    p = pyaudio.PyAudio()\n",
      "    \n",
      "    stream = p.open(format=p.get_format_from_width(sample_width),\n",
      "                    channels=channels,\n",
      "                    rate=framerate,\n",
      "                    output=True)\n",
      "    for frame in frames:\n",
      "        stream.write(frame)\n",
      "        \n",
      "    stream.stop_stream()\n",
      "    stream.close()\n",
      "    p.terminate()\n",
      "\n",
      "def write_frames(frames, filename):\n",
      "    wav_file=wave.open(filename,\"w\")\n",
      "    sampwidth = 2\n",
      "    comptype= \"NONE\"\n",
      "    compname= \"not compressed\"\n",
      "    wav_file.setparams((CHANNELS, sampwidth, RATE, len(frames), comptype, compname))\n",
      "    for s in frames:\n",
      "        wav_file.writeframes(s)\n",
      "    wav_file.close()\n",
      "\n",
      "def play_file(filename):\n",
      "    CHUNK = 1024\n",
      "    \n",
      "    wf = wave.open(filename, 'rb')\n",
      "    \n",
      "    p = pyaudio.PyAudio()\n",
      "    \n",
      "    stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),\n",
      "                    channels=wf.getnchannels(),\n",
      "                    rate=wf.getframerate(),\n",
      "                    output=True)\n",
      "    \n",
      "    data = wf.readframes(CHUNK)\n",
      "    \n",
      "    while data != '':\n",
      "        stream.write(data)\n",
      "        data = wf.readframes(CHUNK)\n",
      "    \n",
      "    stream.stop_stream()\n",
      "    stream.close()\n",
      "    \n",
      "    p.terminate()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "avgs = [np.median(l) for l in list(quietnet.chunks(freq_samples, FRAME_LENGTH))]\n",
      "\n",
      "setup_graph(title='Frequency f Interest over Time', x_label='Time', y_label='Frequency')\n",
      "plot(avgs, 'grey')\n",
      "plot(avgs, '.')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "NameError",
       "evalue": "name 'quietnet' is not defined",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-2-c8d8fe51ae9f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mavgs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmedian\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ml\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mquietnet\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchunks\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfreq_samples\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mFRAME_LENGTH\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0msetup_graph\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtitle\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'Frequency f Interest over Time'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx_label\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'Time'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_label\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'Frequency'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mavgs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'grey'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mavgs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
        "\u001b[0;31mNameError\u001b[0m: name 'quietnet' is not defined"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = '\\xae'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "16 * 16"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "256"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pyaudio\n",
      "import wave\n",
      "WAVE_OUTPUT_FILENAME = \"wave.out\"\n",
      "FORMAT = pyaudio.paInt16\n",
      "p = pyaudio.PyAudio()\n",
      "\n",
      "\n",
      "wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')\n",
      "wf.setnchannels(1)\n",
      "wf.setsampwidth(p.get_sample_size(FORMAT))\n",
      "wf.setframerate(RATE)\n",
      "wf.writeframes(b''.join(buffers))\n",
      "wf.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "NameError",
       "evalue": "name 'buffers' is not defined",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-7-3760e998dc19>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0mwf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetsampwidth\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_sample_size\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mFORMAT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m \u001b[0mwf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetframerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mRATE\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mwf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwriteframes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mb''\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbuffers\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     13\u001b[0m \u001b[0mwf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
        "\u001b[0;31mNameError\u001b[0m: name 'buffers' is not defined"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "44 * 5 * 200"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "44000"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}